package com.onpositive.dataminer.rcp.views;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;

import org.eclipse.jface.viewers.StructuredSelection;

import com.onpositive.commons.elements.AbstractUIElement;
import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.serializer.writers.AbstractWriter;
import com.onpositive.data.serializer.writers.ObjectCollectionWriter;
import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.dataminer.rcp.utils.WriterParams;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.knowledge.model.IUniverse;
import com.onpositive.mediawiki.dataminer.WikiThing;
import com.onpositive.mediawiki.serializing.UniverseObjectCollection;
import com.onpositive.mediawiki.serializing.WriterFactory;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.property.PropertyAccess;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.api.relation.HasValueTransientRelation;
import com.onpositive.semantic.model.api.relation.IRelation;
import com.onpositive.semantic.model.tree.IClusterizationPoint;
import com.onpositive.semantic.model.tree.ITreeNode;
import com.onpositive.semantic.model.tree.RealmNode;
import com.onpositive.semantic.model.tree.RelationClusterizationPoint;
import com.onpositive.semantic.model.ui.property.editors.structured.ListEnumeratedValueSelector;
import com.onpositive.semantic.model.ui.roles.WidgetRegistry;
import com.onpositive.semantic.ui.workbench.elements.XMLView;

public class ObjectViewPart extends XMLView {

	public ObjectViewPart() {
		super("ui/objectView.dlf");
		setSelectionProvider("obj");
	}

	IRealm<IThing> getObjects(){
		return DefaultUniverse.getInstance().all();
	}
	
	public void depricate(){
		AbstractUIElement<?> lst = this.getElement("obj") ;
		ListEnumeratedValueSelector<?> aevs = (ListEnumeratedValueSelector<?>) lst ;
		StructuredSelection sel = (StructuredSelection) aevs.getSelection() ;
		Object[] array = sel.toArray();
		doDepricate(array);
		System.out.println(Arrays.toString(array));
	}

	protected void doDepricate(Object[] array) {
		for (Object o:array){
			if (o instanceof RealmNode){
				RealmNode<?>r=(RealmNode<?>) o;
				doDepricate(r.getChildren());
			}
			if (o instanceof ITreeNode){
				ITreeNode<?>r=(ITreeNode<?>)o;
				o=r.getElement();
			}
			IProperty property = PropertyAccess.getProperty(o, "Depricated");
			if (property!=null){
				PropertyAccess.setValue(property, o, true);
			}			
		}
	}
	
	public void serialize()
	{
		AbstractUIElement<?> lst = this.getElement("obj") ;
		ListEnumeratedValueSelector<?> aevs = (ListEnumeratedValueSelector<?>) lst ;
		StructuredSelection sel = (StructuredSelection) aevs.getSelection() ;
		Object selObj = sel.getFirstElement() ;
		WikiThing root= null ;
		if( selObj instanceof RealmNode<?> )
		{
			 IClusterizationPoint<?> point = ((RealmNode<?>)selObj).getElement() ;
			 if( point instanceof RelationClusterizationPoint<?> )
			 {
				 IRelation relation = ((RelationClusterizationPoint<?>)point).getRelation() ;
				 if( relation instanceof HasValueTransientRelation )
				 {
					 Object finalValue = ((HasValueTransientRelation)relation).getValue() ;
					 if( finalValue instanceof WikiThing )
						 root = (WikiThing) finalValue ;
				 }
			 }
		}
		else if( selObj instanceof WikiThing )
			root = (WikiThing) selObj ;
		else
			return ;

		IUniverse  universe = DefaultUniverse.getInstance() ;
		UniverseObjectCollection collection = new UniverseObjectCollection( universe, root ) ; 
		ObjectCollectionWriter colWriter = new ObjectCollectionWriter() ;
		final WriterParams initial = new WriterParams();
		boolean createObject = WidgetRegistry.createObject(initial);
		if (!createObject){
			return;
		}
		colWriter.setFactory( new WriterFactory() {
			@Override
			public AbstractWriter<?> createWriter(IPropertyMetadata meta) {
				
				String id = meta.id();
				
				if (id!=null&&id.equals(IPropertyConstants.TEXT_CONTENT_PROPERTY_NAME)){
					if (!initial.isWriteText()){
						return null;
					}
				}
				if (id!=null&&id.equals(IPropertyConstants.MAIN_PICTURE_PROPERTY_METADATA_ID)){
					if (!initial.isWriteMainImages()){
						return null;
					}
				}
				if (id!=null&&id.equals(IPropertyConstants.PREVIEW_PICTURE_PROPERTY_METADATA_ID)){
					if (!initial.isWritePreviewImages()){
						return null;
					}
				}
				
				AbstractWriter<?> createWriter = super.createWriter(meta);
				if(createWriter==null)
					return null;				
								
				if(id==null)
					return createWriter;							
				
				return createWriter;
			}
			
		}) ;
		colWriter.append( collection ) ;
		
		try {
			File fl = DefaultUniverse.getInstance().getSerializationLocation() ;
			File f = new File( "C:\\work\\android\\com.onpositive.fighterspedia\\assets\\serialized.awb") ;
			f.getParentFile().mkdirs();
			
			FileOutputStream fos;
			fos = new FileOutputStream( f );
			DataOutputStream dos = new DataOutputStream( fos ) ;
			colWriter.serialize( dos ) ;
			dos.close() ;
			fos.close() ;

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
	}

}
